package com.four.dailymanage.office.service;

import com.four.dailymanage.common.bean.Page;
import com.four.dailymanage.office.dao.OfficeDao;
import com.four.dailymanage.office.entity.Office;
import com.four.dailymanage.repair.entity.RepairApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OfficeService {
    @Autowired
    OfficeDao officeDao;
    //分页
    public Page<Office> getOfficeApply (int pageNum, int pageSize, String handleMan,String examineStatus , String applyType){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Office> list=officeDao.getOfficeByCon(start,pageSize,handleMan,examineStatus,applyType);
        //获取总条目数
        int total=officeDao.countOffice(handleMan,examineStatus,applyType);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Office> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //添加
    public boolean add(Office office){
        if (office == null){
            return false;
        }
        office.setExamineStatus("未审核");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString=sdf.format(new Date());
        office.setApplyDt(dataTimeString);
        return officeDao.addOffice(office)>0;
    }
    //审核通过
  public  boolean approve(Office office){
        if (office == null){
            return false;
        }
        office.setExamineStatus("审核通过");
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dataTimeString=sdf.format(new Date());
      office.setSubmitDt(dataTimeString);
      return officeDao.updateById(office)>0;
  }

    //审核拒绝
    public  boolean reject(Office office){
        if (office == null){
            return false;
        }
        office.setExamineStatus("审核拒绝");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString=sdf.format(new Date());
        office.setSubmitDt(dataTimeString);
        return officeDao.updateById(office)>0;
    }
}
